package cn.edu.ctbu.dp01.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
@Slf4j
public class HttpAspect {
    @Pointcut("execution(* cn.edu.ctbu.dp01.api.StudentRestAplController.*(..))")
    public void log()
    {

    }
    @Before("log()")
    public void doBefore()
    {
        log.info("before");
    }
    @After("log()")
    public void doAfter()
    {
        log.info("after");
    }

    @AfterReturning(returning = "object",pointcut = "log()")
    public void AfterR(Object object)
    {
        log.info("response:{}",object);
    }

    @Around("log()")
    public Object Around(ProceedingJoinPoint pjp) throws Throwable
    {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object obj = pjp.proceed(pjp.getArgs());
        stopWatch.stop();
        long cost = stopWatch.getTotalTimeMillis();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
        log.info("method:{} cost:{}",methodName,cost);
        return obj;
    }
}
